<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/27
 * Time: 16:58
 */

namespace app\index\controller;
use think\Db;
class User extends Base
{
    protected $userId;
    public function _initialize(){
        parent::_initialize();
        $this->userId=cookie('user_id');
    }
    public function index(){
        $user=Db::name('users')->where('id',$this->userId)->find();
        $this->assign([
            'user' => $user
        ]);
        return view();
    }
    // 用户实名制
    public function real(){
        if (request()->isPost()){ // 保存资料
            $data=input('post.');
            $res=Db::name('users')->where('id',$this->userId)->update($data);
            if ($res){
                Db::name('users')->where('id',$this->userId)->setField('is_real',1);
                return json(['msg' => '保存成功' ,'state' => 1]);
            }
            return json(['msg' => '保存失败','state' => -1]);
        }
        $info=Db::name('users')->where('id',$this->userId)->find();
        $this->assign([
            'info' => $info,
        ]);
        return view();
    }
    // 我的卖单
    public function sell(){
        $order=Db::name('order')->where('user_id',$this->userId)->order('add_time desc')->where('type','sell')->select();
        $this->assign([
            'order' => $order,
            'isConfirm' => Db::name('order')->where('user_id',$this->userId)->order('add_time desc')->where('type','sell')->where('state',0)->select(),
            'isPay' => Db::name('order')->where('user_id',$this->userId)->order('add_time desc')->where('type','sell')->where('state',1)->select(),
            'isComplete' => Db::name('order')->where('user_id',$this->userId)->order('add_time desc')->where('type','sell')->where('state',2)->select(),
        ]);
        return view();
    }
    // 创建卖单
    public function newSell(){
        if (request()->isPost()){
            if (date('H') < 9 || date('H') >= 21){
                return json(['msg' => '未在交易时段' ,'code' => -6]);
            }
            $trade_password=input('post.trade_password');
            $user=Db::name('users')->where('id',$this->userId)->find();
            $sell_num=input('post.sell_num');// 要卖出的币
            $account_num=input('post.account_num');// 到账数量
            $unit_price=input('post.unit_price');// 单价
            if ($unit_price > getEndPrice() || $unit_price < getStartPrice()){
                return json(['msg' => '今日价格区间为'.getStartPrice().'元-'.getEndPrice().'元']);
            }
            $fee=input('post.fee');
            $sum_money=input('post.sum_money');
            if ($trade_password != $user['trade_password']){
                return json(['msg' => '交易密码不正确','state' => -1,'code' => -1]);
            }
            if (strlen($user['trade_password']) != 6){
                return json(['msg' => '请设置6位交易密码','code' => -3]);
            }
            if ($sell_num > $user['money']){
                return json(['msg' => 'HX余额不足,不可创建卖单','state' => 1,'code' => -2]);
            }
            Db::name('order')->insert([
                'user_id' => $this->userId,
                'user_phone' => $user['phone'],
                'type' => 'sell',
                'money' => $sell_num*0.8,
                'unit_price' => $unit_price,
                'trade_type' => '银联',
                'add_time' => time(),
                'sum_price' => $sum_money,
                'account_num' => $account_num,
                'fee' => $fee,
            ]);
            Db::name('users')->where('id',$this->userId)->setInc('frozen_money',$sell_num);
            Db::name('users')->where('id',$this->userId)->setDec('money',$sell_num);
            return json(['msg' => '卖单创建成功' ,'state' => -2 , 'code' => 2]);
        }
        $user=Db::name('users')->where('id',$this->userId)->find();
        if ($user['is_real'] == 0){
            $this->redirect('/index/user/real');
        }
        $this->assign(['user' => $user]);
        return view('new_sell');
    }
    // 我的买单
    public function buy(){
        $order=Db::name('order')->where('user_id',$this->userId)->order('add_time desc')->where('type','buy')->select();
        $this->assign([
            'buyList' => $order,
            'isConfirm' => Db::name('order')->where('user_id',$this->userId)->order('add_time desc')->where('type','buy')->where('state',0)->select(),
            'isPay' => Db::name('order')->where('user_id',$this->userId)->order('add_time desc')->where('type','buy')->where('state',1)->select(),
            'isComplete' => Db::name('order')->where('user_id',$this->userId)->order('add_time desc')->where('type','buy')->where('state',2)->select(),
        ]);
        return view();
    }
    // 新建买单
    public function newBuy(){
        if (request()->isPost()){
            if (date('H') < 9 || date('H') >= 21){
                return json(['msg' => '未在交易时段' ,'code' => -6]);
            }
            $trade_password=input('post.trade_password');
            $user=Db::name('users')->where('id',$this->userId)->find();
            $sell_num=input('post.buy_num');// 要买的币
            $unit_price=input('post.unit_price');// 单价
            $sum_money=input('post.sum_money');
            if ($unit_price > getEndPrice() || $unit_price < getStartPrice()){
                return json(['msg' => '今日价格区间为'.getStartPrice().'元-'.getEndPrice().'元']);
            }
            if ($trade_password != $user['trade_password']){
                return json(['msg' => '交易密码不正确','state' => -1,'code' => -1]);
            }
            if (strlen($user['trade_password']) != 6){
                return json(['msg' => '请设置6位交易密码','code' => -3]);
            }
            Db::name('order')->insert([
                'user_id' => $this->userId,
                'user_phone' => $user['phone'],
                'type' => 'buy',
                'money' => $sell_num,
                'unit_price' => $unit_price,
                'trade_type' => '银联',
                'add_time' => time(),
                'sum_price' => $sum_money,
            ]);
            return json(['msg' => '买单创建成功' ,'state' => 1 , 'code' => 2]);
        }
        $user=Db::name('users')->where('id',$this->userId)->find();
        if ($user['is_real'] == 0){
            $this->redirect('/index/user/real');
        }
        $this->assign(['user' => $user]);
        return view('new_buy');
    }
    // 支付信息
    public function payInfo(){
        $this->assign('info',Db::name('users')->where('id',$this->userId)->field('weixin,alipay')->find());
        return view('pay_info');
    }
    // 保存用户的支付宝信息
    public function saveAlipay(){
        if (request()->isPost()){
            $nickname=input('post.nickname');
            $account=input('post.account');
            Db::name('users')->where('id',$this->userId)->update(['alipay' => $account,'alipay_name' => $nickname]);
            return json(['msg' => '已保存','code' => 1]);
        }
        return view('save_alipay');
    }
    // 保存用户的微信信息
    public function saveWeChat(){
        if (request()->isPost()){
            $nickname=input('post.nickname');
            $account=input('post.account');
            Db::name('users')->where('id',$this->userId)->update(['weixin' => $account,'weixin_name' => $nickname]);
            return json(['msg' => '已保存','code' => 1]);
        }
        return view('save_we_chat');
    }
}